package com.tianma.sync;

import com.tianma.entity.TradeOrdersPicking;
import com.tianma.service.TradeOrdersPickingService;
import com.tianma.model.BinlogValue;
import com.tianma.util.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class TradeOrdersPickingSync extends AbstractSyncTemplate {

    @Autowired
    private TradeOrdersPickingService pickingService;

    @Override
    void insert(List<Map<String, BinlogValue>> rows) throws Exception {
        saveOrUpdate(rows);
    }

    @Override
    void update(List<Map<String, BinlogValue>> rows) throws Exception {
        saveOrUpdate(rows);
    }

    @Override
    void delete(List<Map<String, BinlogValue>> rows) {
        for (Map<String, BinlogValue> row : rows) {
            BinlogValue id = row.get("id");
            pickingService.removeById(id.getValue());
            log.debug("删除 trade_orders_picking 记录：", id);
        }
    }

    /**
     * 将数据记录插入或更新到目标MySQL中
     *
     * @param rows
     * @throws Exception
     */
    private void saveOrUpdate(List<Map<String, BinlogValue>> rows) throws Exception {
        for (Map<String, BinlogValue> row : rows) {
            BinlogValue id = row.get("id");
            BinlogValue orderId = row.get("order_id");
            BinlogValue supplierId = row.get("supplier_id");
            BinlogValue warehouseId = row.get("warehouse_id");
            BinlogValue warehouseName = row.get("warehouse_name");
            BinlogValue pickingStatus = row.get("picking_status");
            BinlogValue pickingDatetime = row.get("picking_datetime");
            BinlogValue pickingRemark = row.get("picking_remark");
            BinlogValue delivery = row.get("delivery");
            BinlogValue deliveryNo = row.get("delivery_no");
            BinlogValue deliveryDate = row.get("delivery_date");
            BinlogValue feedbackDatetime = row.get("feedback_datetime");
            BinlogValue feedbackRemark = row.get("feedback_remark");
            BinlogValue warehouseGoodsNo = row.get("warehouse_goods_no");
            BinlogValue warehouseSize = row.get("warehouse_size");
            BinlogValue thirdExtraInfo = row.get("third_extra_info");
            BinlogValue warehousePrice = row.get("warehouse_price");
            BinlogValue warehouseDiscount = row.get("warehouse_discount");
            BinlogValue supplierWareId = row.get("supplier_ware_id");

            TradeOrdersPicking picking = new TradeOrdersPicking();
            picking.setId(Long.parseLong(id.getValue()));
            picking.setOrderId(Long.parseLong(orderId.getValue()));
            picking.setSupplierId(Integer.parseInt(supplierId.getValue()));
            picking.setWarehouseId(Integer.parseInt(warehouseId.getValue()));
            picking.setWarehouseName(warehouseName.getValue());
            picking.setPickingStatus(Integer.parseInt(pickingStatus.getValue()));
            picking.setPickingDatetime(DateUtils.parseDatetime(pickingDatetime.getValue()));
            picking.setPickingRemark(pickingRemark.getValue());
            picking.setDelivery(delivery.getValue());
            picking.setDeliveryNo(deliveryNo.getValue());
            picking.setDeliveryDate(DateUtils.parseDatetime(deliveryDate.getValue()));
            picking.setFeedbackDatetime(DateUtils.parseDatetime(feedbackDatetime.getValue()));
            picking.setFeedbackRemark(feedbackRemark.getValue());
            picking.setWarehouseGoodsNo(warehouseGoodsNo.getValue());
            picking.setWarehouseSize(warehouseSize.getValue());
            picking.setThirdExtraInfo(thirdExtraInfo.getValue());
            picking.setWarehousePrice(Double.parseDouble(StringUtils.isNotEmpty(warehousePrice.getValue()) ? warehousePrice.getValue() : "0"));
            picking.setWarehouseDiscount(Double.parseDouble(StringUtils.isNotEmpty(warehouseDiscount.getValue()) ? warehouseDiscount.getValue() : "0"));
            picking.setSupplierWareId(Integer.parseInt(supplierWareId.getValue()));

            log.debug("插入或更新 trade_orders_picking 数据:", picking);
            pickingService.saveOrUpdate(picking);
        }
    }
}
